package m.olive.data.dataparese;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import m.framework.exception.SystemException;
import m.framework.utils.http.HttpUtil;

import org.apache.http.client.HttpClient;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ming
 * @date 2014年2月24日下午4:49:20
 */
public class TomcatStatus {
	private static Logger log = LoggerFactory.getLogger(TomcatStatus.class);
	public static Set<String> keys = new HashSet();
	private static Map<String, HttpClient> httpclientMap = new HashMap();
	private static Map<String, String> memorypool = new HashMap();
	static {
		memorypool.put("PS Eden Space", "eden");
		memorypool.put("PS Old Gen", "old");
		memorypool.put("PS Survivor Space", "survivor");
		memorypool.put("Code Cache", "code");
		memorypool.put("PS Perm Gen", "perm");

		keys.add("eden_usageMax");
		keys.add("old_usageMax");
		keys.add("survivor_usageMax");
		keys.add("code_usageMax");
		keys.add("perm_usageMax");

		keys.add("eden_usageUsed");
		keys.add("old_usageUsed");
		keys.add("survivor_usageUsed");
		keys.add("code_usageUsed");
		keys.add("perm_usageUsed");

		keys.add("thread_max");
		keys.add("thread_count");
		keys.add("thread_busy");

		keys.add("memery_free");
		keys.add("memery_total");
		keys.add("memery_max");
	}

	public TomcatStatus() {

	}

	public Map<String, String> getValues(Map<String, String> params) {
		String url = params.get("url");
		String userName = params.get("userName");
		String password = params.get("password");
		String port = params.get("port");

		HttpClient httpclient = this.getHttpClient(userName, password);
		String xml = HttpUtil.get(httpclient, url);
		try {
			return parseXml(xml, port);
		} catch (Exception ex) {
			throw new SystemException("解析xml文件时出错:" + xml, ex);
		}

	}

	/**
	 * status jvm memory(free,total,max) memorypool() connector threadInfo
	 * requestInfo workers
	 * 
	 * */
	private Map<String, String> parseXml(String xml, String port) throws Exception {
		Map map = new HashMap();
		SAXReader reader = new SAXReader();
		byte[] byts = xml.getBytes(HttpUtil.DEFAULT_ENCODE);
		ByteArrayInputStream bis = new ByteArrayInputStream(byts);
		Document document = reader.read(bis);
		Element root = document.getRootElement();
		for (Iterator<Element> it = root.elementIterator(); it.hasNext();) {
			Element el = it.next();
			if (el.getName().equals("jvm")) {
				addJvm(el, map);
			}
			if (el.getName().equals("connector")) {
				if (el.attributeValue("name").indexOf(port) != -1) {
					Element thread = (Element) el.elementIterator("threadInfo").next();
					map.put("thread_max", thread.attributeValue("maxThreads"));
					map.put("thread_count", thread.attributeValue("currentThreadCount"));
					map.put("thread_busy", thread.attributeValue("currentThreadsBusy"));
				}
			}

		}
		return map;
	}

	// 解析jvm
	private void addJvm(Element jvmEl, Map map) {
		for (Iterator<Element> itt = jvmEl.elementIterator(); itt.hasNext();) {
			Element ell = itt.next();
			if (ell.getName().equals("memory")) {
				map.put("memery_free", ell.attributeValue("free"));
				map.put("memery_total", ell.attributeValue("total"));
				map.put("memery_max", ell.attributeValue("max"));
			}
			if (ell.getName().equals("memorypool")) {
				String name = ell.attributeValue("name");
				name = memorypool.get(name);
				map.put(name + "_usageMax", ell.attributeValue("usageMax"));
				map.put(name + "_usageUsed", ell.attributeValue("usageUsed"));
			}
		}
	}

	private HttpClient getHttpClient(String userName, String password) {
		String key = userName + "-" + password;
		HttpClient c = httpclientMap.get(key);
		if (c == null) {
			c = HttpUtil.createClient(userName, password, 10000, 20000);
			httpclientMap.put(key, c);
		}
		return c;
	}

}
